package org.example.system.domain.bo;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.example.common.core.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 客户端信息
 */
@Data
public class SysClientBo implements Serializable {

    private static final long serialVersionUID = -6947822646185526939L;
    /**
     * 客户端ID
     */
    private String clientId;
    /**
     * 客户端的秘钥密文secret
     */
    private String clientSecret;
    /**
     * 授权登录方式
     */
    private List<String> grantTypes;
    /**
     * 授权码模式的跳转uri
     */
    private String webServerRedirectUri;
    /**
     * 权限,多个用逗号分隔
     */
    private String authorities;

    /**
     * access_token的过期时间，单位毫秒，覆盖掉硬编码
     */
    private Integer accessTokenValidity;

    /**
     * refresh_token的过期时间，单位毫秒，覆盖掉硬编码
     */
    private Integer refreshTokenValidity;
    /**
     *    optional扩展字段，JSON
     */
    private String additionalInformation;
    /**
     * The client is trusted or not. If it is trust, will skip approve step
     * default false.
     */
    private boolean trusted = false;
    /**
     * Value is 'true' or 'false',  default 'false'
     */
    private String autoapprove;
    /**
     * 描述
     */
    private String remark;
    /**
     * 授权设备
     */
    private String deviceType;
    /**
     * 授权用户
     */
    private String grantUser;

    private String status;


    public SysOauthClientBo createDomainBo(){
        SysOauthClientBo clientBo=new SysOauthClientBo();
        BeanUtils.copyProperties(this,clientBo);
        if(!CollectionUtils.isEmpty(this.getGrantTypes())&&this.getGrantTypes().size()>0){
            clientBo.setAuthorizedGrantTypes(StringUtils.join(this.getGrantTypes(),","));
        }
        JSONObject AdditionalInformation=new JSONObject();
        if(ObjectUtil.isNotEmpty(this.getDeviceType())){
            AdditionalInformation.put("device_type",this.getDeviceType());
        }
        if(ObjectUtil.isNotEmpty(this.getGrantUser())){
            AdditionalInformation.put("grant_user",this.getGrantUser());
        }
        if(AdditionalInformation.keySet().size()>0){
            clientBo.setAdditionalInformation(AdditionalInformation.toJSONString());
        }
        return clientBo;
    }

}
